package com.english.utils;

import java.security.MessageDigest;
import java.util.Map;
import java.util.TreeMap;

/**
 * 微信支付签名调试工具
 */
public class WechatPaySignatureDebugger {
    
    public static void main(String[] args) {
        // 从日志中提取的实际参数
        Map<String, String> params = new TreeMap<>();
        params.put("appid", "wx2f9ca3ee9556234f");
        params.put("body", "课时充值 - 4000元");
        params.put("mch_id", "1724666825");
        params.put("nonce_str", "AADFCA664D074C15A814AED84ADF0EB4");
        params.put("notify_url", "https://a381e5d9d03a.ngrok-free.app/api/payment/notify");
        params.put("openid", "opMFNvowB-lDGIOGm7cCewHVRTG4");
        params.put("out_trade_no", "PAY1754915627541264");
        params.put("sign_type", "MD5");
        params.put("spbill_create_ip", "127.0.0.1");
        params.put("total_fee", "400000");
        params.put("trade_type", "JSAPI");
        
        String apiKey = "18308909937or18970222333Oscar168";
        String expectedSign = "46B030CD4882BAE150853C2810C66A04";
        
        System.out.println("=== 微信支付签名调试 ===");
        System.out.println("API密钥长度: " + apiKey.length());
        System.out.println("期望签名: " + expectedSign);
        
        // 生成签名字符串
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (entry.getValue() != null && !entry.getValue().isEmpty() && !"sign".equals(entry.getKey())) {
                sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }
        sb.append("key=").append(apiKey);
        
        String signString = sb.toString();
        System.out.println("\n签名字符串:");
        System.out.println(signString);
        
        // 计算MD5
        String calculatedSign = md5(signString);
        System.out.println("\n计算得到的签名: " + calculatedSign);
        System.out.println("期望的签名:     " + expectedSign);
        System.out.println("签名匹配: " + expectedSign.equals(calculatedSign));
        
        // 分析参数
        System.out.println("\n=== 参数分析 ===");
        for (Map.Entry<String, String> entry : params.entrySet()) {
            System.out.println(entry.getKey() + " = " + entry.getValue());
        }
        
        // 测试不同的API密钥长度
        System.out.println("\n=== API密钥分析 ===");
        System.out.println("当前API密钥: " + apiKey);
        System.out.println("长度: " + apiKey.length() + " (应该是32位)");
        System.out.println("包含非字母数字字符: " + !apiKey.matches("^[a-zA-Z0-9]+$"));
        
        // 尝试截取32位
        if (apiKey.length() > 32) {
            String truncatedKey = apiKey.substring(0, 32);
            System.out.println("\n尝试使用前32位: " + truncatedKey);
            String signWithTruncated = generateSignWithKey(params, truncatedKey);
            System.out.println("使用前32位的签名: " + signWithTruncated);
        }
    }
    
    private static String generateSignWithKey(Map<String, String> params, String apiKey) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (entry.getValue() != null && !entry.getValue().isEmpty() && !"sign".equals(entry.getKey())) {
                sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }
        sb.append("key=").append(apiKey);
        return md5(sb.toString());
    }
    
    public static String md5(String data) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] array = md.digest(data.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            for (byte item : array) {
                sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
            }
            return sb.toString().toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
} 